/**
* Copyright (C) 2018-2020
* All rights reserved, Designed By www.yixiang.co
* 注意：
* 本软件为www.yixiang.co开发研制，未经购买不得使用
* 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
* 一经发现盗用、分享等行为，将追究法律责任，后果自负
*/
package co.yixiang.retail.biz.log.service.impl;

import co.yixiang.common.core.utils.FileUtil;
import co.yixiang.common.mproot.common.service.impl.BaseServiceImpl;
import co.yixiang.common.mproot.common.utils.QueryHelpPlus;
import co.yixiang.common.mproot.domain.PageResult;
import co.yixiang.common.mproot.dozer.service.IGenerator;
import co.yixiang.retail.biz.log.mapper.YxStoreServiceChargeLogMapper;
import co.yixiang.retail.biz.log.service.YxStoreServiceChargeLogService;
import co.yixiang.retail.common.domain.YxStoreServiceChargeLog;
import co.yixiang.retail.common.dto.YxStoreServiceChargeLogDto;
import co.yixiang.retail.common.dto.YxStoreServiceChargeLogQueryCriteria;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;

/**
 * @author poppy_hu
 * @date 2021-04-21
 */
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "yxStoreServiceChargeLog")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxStoreServiceChargeLogServiceImpl extends BaseServiceImpl<YxStoreServiceChargeLogMapper, YxStoreServiceChargeLog> implements YxStoreServiceChargeLogService {

    private final IGenerator generator;

    @Override
    //@Cacheable
    public PageResult<YxStoreServiceChargeLogDto> queryAll(YxStoreServiceChargeLogQueryCriteria criteria, Pageable pageable) {
        getPage(pageable);
        PageInfo<YxStoreServiceChargeLog> page = new PageInfo<>(queryAll(criteria));
        return generator.convertPageInfo(page,YxStoreServiceChargeLogDto.class);
    }


    @Override
    //@Cacheable
    public List<YxStoreServiceChargeLog> queryAll(YxStoreServiceChargeLogQueryCriteria criteria){
        return baseMapper.selectList(QueryHelpPlus.getPredicate(YxStoreServiceChargeLog.class, criteria));
    }


    @Override
    public void download(List<YxStoreServiceChargeLogDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YxStoreServiceChargeLogDto yxStoreServiceChargeLog : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("门店id", yxStoreServiceChargeLog.getStoreId());
            map.put("产品id", yxStoreServiceChargeLog.getProId());
            map.put("产品名称", yxStoreServiceChargeLog.getProName());
            map.put("产品价格", yxStoreServiceChargeLog.getProPrice());
            map.put("订单id", yxStoreServiceChargeLog.getOrderId());
            map.put("购买产品数量", yxStoreServiceChargeLog.getProNum());
            map.put("购买人id", yxStoreServiceChargeLog.getBuyerId());
            map.put("购买人姓名", yxStoreServiceChargeLog.getBuyerName());
            map.put("购买人手机号", yxStoreServiceChargeLog.getBuyerPhone());
            map.put("服务费", yxStoreServiceChargeLog.getServiceCharge());
            map.put("添加时间", yxStoreServiceChargeLog.getCreateTime());
            map.put("门店名称", yxStoreServiceChargeLog.getStoreName());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
